package main

import (
	"github.com/therecipe/qt/widgets"
	m"github.com/therecipe/qt/multimedia"
	"goplayer/list"
	"net/http"
	"fmt"
	"io/ioutil"
	"encoding/json"
	"goplayer/config"
)

type CentralWidget struct{
	widgets.QStackedWidget
	Playlistid int
	Player *m.QMediaPlayer
	PlayList *m.QMediaPlaylist
	songsView *list.SongsView
	_ func() `constructor:"init"`
}

func (this *CentralWidget) SetNet(){
	req,_ := http.NewRequest("POST",config.BaseUrl+"api/song/?expand=album",nil)
	req.Header.Set("Content-Type", "application/json")
	client := http.DefaultClient
	resp,err := client.Do(req)
	if err != nil{
		fmt.Println("网络错误",err)
	}
	retbody := resp.Body
	defer retbody.Close()
	if resp.StatusCode != 200 {
		fmt.Errorf("HTTP Response Error %d\n",resp.StatusCode)
	}
	retbodyr, _ := ioutil.ReadAll(retbody)
	var songdatas  []*list.SongData
	json.Unmarshal(retbodyr,&songdatas)
	widgets.QMessageBox_Information(nil, "OK", fmt.Sprint(songdatas),
		widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
	this.songsView.SetData(songdatas,0)
}

func (this *CentralWidget) init() {
	player := m.NewQMediaPlayer(this,m.QMediaPlayer__StreamPlayback)
	playList := m.NewQMediaPlaylist(this)
	player.SetPlaylist(playList)
	songsView := list.NewSongsView(nil,0)
	this.AddWidget(songsView)
	this.Player = player
	this.PlayList = playList
	this.songsView = songsView
	this.SetNet()
}